package com.ugoodtech.mdcc.client.service.impl;

import com.mysema.query.BooleanBuilder;
import com.ugoodtech.mdcc.client.dto.JsonResponse;
import com.ugoodtech.mdcc.client.dto.ServiceTargetVO;
import com.ugoodtech.mdcc.client.service.QuestionService;
import com.ugoodtech.mdcc.core.domain.*;
import com.ugoodtech.mdcc.core.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    private QuestionSectionRepository questionSectionRepository;

    @Autowired
    private QuestionGroupRepository questionGroupRepository;

    @Autowired
    private QuestionPaperRepository questionPaperRepository;

    @Autowired
    private QuestionRepository questionRepository;

    @Autowired
    private QuestionAnswerOptionRepository questionAnswerOptionRepository;




    /**
     * 获取筛选的条件--常规报告中的服务项目
     */
    @Override
    public JsonResponse getServiceTarget() {
        QQuestionAnswerOption qQuestionAnswerOption = QQuestionAnswerOption.questionAnswerOption;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qQuestionAnswerOption.deleted.eq(false));
        builder.and(qQuestionAnswerOption.enabled.eq(true));
        builder.and(qQuestionAnswerOption.question.id.eq(155L));
        Page<QuestionAnswerOption> questionAnswerOptionList = questionAnswerOptionRepository.findAll(builder,new PageRequest(0, 1000, new Sort(Sort.Direction.ASC, "idx")));
        List<ServiceTargetVO> serviceTargetVOS = new ArrayList<>();
        for (QuestionAnswerOption answer : questionAnswerOptionList) {
            ServiceTargetVO serviceTargetVO = new ServiceTargetVO();
            serviceTargetVO.setAnswerId(answer.getId());
            serviceTargetVO.setName(answer.getName());
            serviceTargetVOS.add(serviceTargetVO);
        }
        return JsonResponse.successResponseWithData(serviceTargetVOS);
    }

    /**
     * 获取筛选的条件--该维修站是否使用我司增值服务
     * @return
     */
    @Override
    public JsonResponse getPayService() {
        QQuestionAnswerOption qQuestionAnswerOption = QQuestionAnswerOption.questionAnswerOption;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qQuestionAnswerOption.deleted.eq(false));
        builder.and(qQuestionAnswerOption.enabled.eq(true));
        builder.and(qQuestionAnswerOption.question.id.eq(208L));
        Page<QuestionAnswerOption> questionAnswerOptionList = questionAnswerOptionRepository.findAll(builder,new PageRequest(0, 1000, new Sort(Sort.Direction.ASC, "idx")));
        List<ServiceTargetVO> serviceTargetVOS = new ArrayList<>();
        for (QuestionAnswerOption answer : questionAnswerOptionList) {
            ServiceTargetVO serviceTargetVO = new ServiceTargetVO();
            serviceTargetVO.setAnswerId(answer.getId());
            serviceTargetVO.setName(answer.getName());
            serviceTargetVOS.add(serviceTargetVO);
        }
        return JsonResponse.successResponseWithData(serviceTargetVOS);
    }

    /**
     * 获取筛选的条件--油漆配送时效
     * @return
     */
    @Override
    public JsonResponse getSentTime() {
        QQuestionAnswerOption qQuestionAnswerOption = QQuestionAnswerOption.questionAnswerOption;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qQuestionAnswerOption.deleted.eq(false));
        builder.and(qQuestionAnswerOption.enabled.eq(true));
        builder.and(qQuestionAnswerOption.question.id.eq(218L));
        Page<QuestionAnswerOption> questionAnswerOptionList = questionAnswerOptionRepository.findAll(builder,new PageRequest(0, 1000, new Sort(Sort.Direction.ASC, "idx")));
        List<ServiceTargetVO> serviceTargetVOS = new ArrayList<>();
        for (QuestionAnswerOption answer : questionAnswerOptionList) {
            ServiceTargetVO serviceTargetVO = new ServiceTargetVO();
            serviceTargetVO.setAnswerId(answer.getId());
            serviceTargetVO.setName(answer.getName());
            serviceTargetVOS.add(serviceTargetVO);
        }
        return JsonResponse.successResponseWithData(serviceTargetVOS);
    }

    /**
     * 获取筛选的条件--经销商上门服务周期
     * @return
     */
    @Override
    public JsonResponse getServiceTime() {
        QQuestionAnswerOption qQuestionAnswerOption = QQuestionAnswerOption.questionAnswerOption;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qQuestionAnswerOption.deleted.eq(false));
        builder.and(qQuestionAnswerOption.enabled.eq(true));
        builder.and(qQuestionAnswerOption.question.id.eq(219L));
        Page<QuestionAnswerOption> questionAnswerOptionList = questionAnswerOptionRepository.findAll(builder,new PageRequest(0, 1000, new Sort(Sort.Direction.ASC, "idx")));
        List<ServiceTargetVO> serviceTargetVOS = new ArrayList<>();
        for (QuestionAnswerOption answer : questionAnswerOptionList) {
            ServiceTargetVO serviceTargetVO = new ServiceTargetVO();
            serviceTargetVO.setAnswerId(answer.getId());
            serviceTargetVO.setName(answer.getName());
            serviceTargetVOS.add(serviceTargetVO);
        }
        return JsonResponse.successResponseWithData(serviceTargetVOS);
    }
    /**
     * 获取筛选的条件--常规报告状态
     * @return
     */
    @Override
    public JsonResponse getReportStatus() {
        QQuestionAnswerOption qQuestionAnswerOption = QQuestionAnswerOption.questionAnswerOption;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qQuestionAnswerOption.deleted.eq(false));
        builder.and(qQuestionAnswerOption.enabled.eq(true));
        builder.and(qQuestionAnswerOption.question.id.eq(156L));
        Page<QuestionAnswerOption> questionAnswerOptionList = questionAnswerOptionRepository.findAll(builder,new PageRequest(0, 1000, new Sort(Sort.Direction.ASC, "idx")));
        List<ServiceTargetVO> serviceTargetVOS = new ArrayList<>();
        for (QuestionAnswerOption answer : questionAnswerOptionList) {
            ServiceTargetVO serviceTargetVO = new ServiceTargetVO();
            serviceTargetVO.setAnswerId(answer.getId());
            serviceTargetVO.setName(answer.getName());
            serviceTargetVOS.add(serviceTargetVO);
        }
        return JsonResponse.successResponseWithData(serviceTargetVOS);
    }


    @Override
    public List<QuestionSection> getQuestionSections(QuestionPaper.QuestionPaperCategory category, Integer owner) {
        QuestionPaper questionPaper = getPaper(category);
        QQuestionSection qQuestionSection = QQuestionSection.questionSection;
        BooleanBuilder builder = new BooleanBuilder();
        if (null != questionPaper) {
            builder.and(qQuestionSection.deleted.eq(false));
            builder.and(qQuestionSection.paper.id.eq(questionPaper.getId()));
            if (owner != null){
                builder.and(qQuestionSection.owner.eq(owner).or(qQuestionSection.owner.eq(2)));
            }
            Page<QuestionSection> page = questionSectionRepository.findAll(builder, new PageRequest(0, 1000, new Sort(Sort.Direction.ASC, "idx")));
            return page.getContent();
        } else {
            return new ArrayList<>();
        }
    }



    @Override
    public List<QuestionGroup> getQuestions(Long sectionId, Integer owner) {
        QQuestionGroup qQuestionGroup = QQuestionGroup.questionGroup;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qQuestionGroup.deleted.eq(false));
        builder.and(qQuestionGroup.questionSection.id.eq(sectionId));
        if (owner != null){
            builder.and(qQuestionGroup.owner.eq(owner).or(qQuestionGroup.owner.eq(2)));
        }
        Page<QuestionGroup> questionGroups = questionGroupRepository.findAll(builder,
                new PageRequest(0, 1000, new Sort(Sort.Direction.ASC, "idx")));
        return questionGroups.getContent();
    }

    @Override
    public List<Question> getQuestionsByGroup(Long groupId, Integer owner) {
        QQuestion qQuestion = QQuestion.question;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qQuestion.deleted.eq(false));
        builder.and(qQuestion.qGroup.id.eq(groupId));
        if (owner != null){
            builder.and(qQuestion.owner.eq(owner).or(qQuestion.owner.eq(2)));
        }
        Page<Question> questions = questionRepository.findAll(builder,
                new PageRequest(0, 1000, new Sort(Sort.Direction.ASC, "idx")));

        return questions.getContent();
    }

    @Override
    public List<QuestionAnswerOption> getOptionsByGroup(Long questionId, Integer owner) {
        QQuestionAnswerOption qQuestionAnswerOption = QQuestionAnswerOption.questionAnswerOption;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qQuestionAnswerOption.deleted.eq(false));
        builder.and(qQuestionAnswerOption.question.id.eq(questionId));
        if (owner != null){
            builder.and(qQuestionAnswerOption.owner.eq(owner).or(qQuestionAnswerOption.owner.eq(2)));
        }
        Page<QuestionAnswerOption> questions = questionAnswerOptionRepository.findAll(builder, new PageRequest(0, 1000, new Sort(Sort.Direction.ASC, "idx")));
        return questions.getContent();
    }

    private QuestionPaper getPaper(QuestionPaper.QuestionPaperCategory category) {
        QQuestionPaper qQuestionPaper = QQuestionPaper.questionPaper;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qQuestionPaper.deleted.eq(false));
        builder.and(qQuestionPaper.category.eq(category));
        Page<QuestionPaper> questionPapers = questionPaperRepository.findAll(builder, new PageRequest(0, 100));
        QuestionPaper questionPaper = null;
        for (QuestionPaper savedQuestionPaper : questionPapers) {
            questionPaper = savedQuestionPaper;
        }
        return questionPaper;
    }
}
